package com.ionehe.group.common.uaa.ex.domain.service.impl;

import com.ionehe.group.common.uaa.ex.domain.service.UserCacheWriteService;
import com.ionehe.group.common.uaa.ex.dto.ForestNodeMerger;
import com.ionehe.group.common.uaa.ex.dto.FunctionDTO;
import com.ionehe.group.common.uaa.ex.dto.UserDetailDTO;
import com.ionehe.group.common.uaa.ex.dto.UserInfoDTO;
import com.ionehe.group.common.uaa.ex.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Copyright (c) 2020 ionehe.com
 * Date: 2020/12/1
 * Time: 11:29 上午
 *
 * @author 2020年 <a href="mailto:a@ionehe.com">秀</a>
 */
@Service
@Slf4j
public class UserCacheWriteServiceImpl implements UserCacheWriteService {
    @Autowired
    private RedisUtils redisUtils;
    @Value("${redis.database:base}")
    private String redisDatabase;
    @Value("${redis.expire.common:86400}")
    private Long redisExpire;
    @Value("${redis.key.user:uaa:user}")
    private String redisKeyUser;
    @Value("${redis.key.resourceList:uaa:resourceList}")
    private String redisKeyResourceList;

    @Override
    public void setUser(UserDetailDTO user) {
        log.info("UserCacheWriteService[]setUser[]start! user:{}", user);
        UserInfoDTO u = user.getUserInfoDTO();
        String key = redisDatabase + ":" + redisKeyUser + ":" + u.getTerminal() + "_" + u.getPhone();
        redisUtils.set(0, key, user, redisExpire);
    }

    @Override
    public void delUser(String account) {
        log.info("UserCacheWriteService[]delUser[]start! account:{}", account);
        String key = redisDatabase + ":" + redisKeyUser + ":" + account;
        redisUtils.delete(0, key);
    }

    @Override
    public void delUser(List<String> accountList) {
        log.info("UserCacheWriteService[]delUser[]start! account:{}", accountList);
        Set<String> keys = accountList.stream().map(account -> redisDatabase + ":" + redisKeyUser + ":" + account).collect(Collectors.toSet());
        redisUtils.batchDelete(keys);
    }

    @Override
    public void setResourceList(String account, List<FunctionDTO> resourceList) {
        log.info("UserCacheWriteService[]setResourceList[]start! account:{}, resourceList:{}", account, resourceList);
        String key = redisDatabase + ":" + redisKeyResourceList + ":" + account;
        redisUtils.set(0, key, resourceList, redisExpire);
    }

    @Override
    public void delResourceList(String account) {
        log.info("UserCacheWriteService[]delResourceList[]start! account:{}", account);
        String key = redisDatabase + ":" + redisKeyResourceList + ":" + account;
        redisUtils.delete(0, key);
    }

    @Override
    public void delResourceList(List<String> accountList) {
        log.info("UserCacheWriteService[]delResourceList[]start! accountList:{}", accountList);
        Set<String> keys = accountList.stream().map(account -> redisDatabase + ":" + redisKeyResourceList + ":" + account).collect(Collectors.toSet());
        redisUtils.batchDelete(keys);
    }

    @Override
    public void delUserModuleCache() {
        log.info("UserCacheWriteService[]delUserModuleCache[]start!");
        redisUtils.batchDeleteByKeyPrefix(redisDatabase + ":uaa");
    }

    @Override
    public void delResourceModuleCache() {
        log.info("UserCacheWriteService[]delResourceModuleCache[]start!");
        redisUtils.batchDeleteByKeyPrefix(redisDatabase + ":" + redisKeyResourceList);
    }

    @Override
    public void delUserToken(List<String> accountList) {
        log.info("UserCacheWriteService[]delUser[]start! account:{}", accountList);
        Set<String> keys = accountList.stream().map(account -> redisDatabase + ":" + redisKeyUser + ":" + account + ":" + "token").collect(Collectors.toSet());
        redisUtils.batchDelete(keys);
    }
}
